package com.jmectf.spatial;

import java.nio.FloatBuffer;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.TexCoords;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
import com.jmex.game.StandardGame;

/**
 * @author Lev Krayzman
 * @version 6/18/09 10:11 PM
 * 
 * This class represents the minimap visible on the game screen.
 */
public class Minimap extends Quad{

	private int textureWidth;
	private int textureHeight;

	StandardGame g;

	/**
	 * The constructor creates the minimap, applies texture from minitexture.png, and
	 * so far does nothing useful but look cool.
	 *  
	 * @param g : <code>StandardGame</code>, needs to be passed to this class to access the game. 
	 */
	public Minimap(StandardGame g){
		//This is the size of the quad. x, y
		super("MiniMap", g.getSettings().getWidth()/5.0f, g.getSettings().getWidth()/5.0f );
		this.g = g;	
		//....magically makes it visible.
		this.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		//Where to put it.
		this.setLocalTranslation((new Vector3f(this.getWidth()*4.4f, this.getHeight()*3.2f, 0.0f)).add(this.getCenter()));
		//Keeps constantly white. Turn off for nice shading effect.
		this.setLightCombineMode(LightCombineMode.Off);
		//this.setDefaultColor(new ColorRGBA(.3f, .3f, .3f, .8f));
		this.applyTexture();
		//Sounds important, so I won't touch it :).
		this.updateRenderState();	
	}

	// Everything from here on has been taken from jmonkeyengine.com, with some minor modifications
	private void applyTexture(){

		// create the texture state to handle the texture
		final TextureState ts = g.getDisplay().getRenderer().createTextureState();
		// load the image bs a texture (the image should be placed in the same directory bs this class)
		final Texture texture = TextureManager.loadTexture(
				// radar.png for completely opaque, radar3.png for slightly translucent
				getClass().getResource("/com/jmectf/data/gui/radar3.png"),
				Texture.MinificationFilter.Trilinear, // of no use for the quad
				Texture.MagnificationFilter.Bilinear, // of no use for the quad
				1.0f,
				true);
		// set the texture for this texture state
		ts.setTexture(texture);
		// initialize texture width
		textureWidth = ts.getTexture().getImage().getWidth();
		// initialize texture height
		textureHeight = ts.getTexture().getImage().getHeight();
		// activate the texture state
		ts.setEnabled(true); 	

		//-----------------------
		// correct texture application:
		final FloatBuffer texCoords = BufferUtils.createVector2Buffer(4);
		// coordinate (0,0) for vertex 0
		texCoords.put(getUForPixel(0)).put(getVForPixel(0));
		// coordinate (0,40) for vertex 1
		texCoords.put(getUForPixel(0)).put(getVForPixel(128));
		// coordinate (40,40) for vertex 2
		texCoords.put(getUForPixel(128)).put(getVForPixel(128));
		// coordinate (40,0) for vertex 3
		texCoords.put(getUForPixel(128)).put(getVForPixel(0));
		// assign texture coordinates to the quad
		this.setTextureCoords(new TexCoords(texCoords));
		// apply the texture state to the quad
		this.setRenderState(ts);

		//--------------------
		// to handle texture transparency:
		// create a blend state
		final BlendState bs = g.getDisplay().getRenderer().createBlendState();
		// activate blending
		bs.setBlendEnabled(true);
		// set the source function
		bs.setSourceFunctionAlpha(BlendState.SourceFunction.SourceAlpha);
		// set the destination function
		bs.setDestinationFunctionAlpha(BlendState.DestinationFunction.OneMinusSourceAlpha);
		// set the blend equation between source and destination
		bs.setBlendEquation(BlendState.BlendEquation.Add);
		bs.setTestEnabled(false);
		// activate the blend state
		bs.setEnabled(true);
		// assign the blender state to the quad
		this.setRenderState(bs);


	}

	private float getUForPixel(int xPixel) {
		return (float) xPixel / textureWidth;
	}

	private float getVForPixel(int yPixel) {
		return 1f - (float) yPixel / textureHeight;
	}

	private static final long serialVersionUID = 1L;

}
